//
//  JigSawButtonStripView.m
//  JigSawKiosk
//
//  Created by shaun on 10-11-03.
//  Copyright 2010 FruitonLabs. All rights reserved.
//

#import "JigSawButtonStripView.h"


extern float kPreviewStopOpacity ;
 

@implementation JigSawButtonStripView

@synthesize stripMovieButtonArray ;

#pragma mark cleanup
-(void) dealloc {
	[stripMovieButtonArray release] ;
	[stripPreviewIterationTimer release] ;
	stripPreviewIterationTimer = nil ;
	[super dealloc] ;
}

#pragma mark initalization


- (id)initWithObjects:(CGRect)_frameSize urls:(NSMutableArray*)urls {
	self = [super init] ;
    if (self) {
        // Initialization code here.
		self.bounds = _frameSize ;
		stripUrlArray = urls ;
		[self setupLayers] ;
		//[self startPreviews] ;
		
    }
    return self;
}

#pragma mark setup
- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
	[[NSColor clearColor] setFill];
    NSRectFill(dirtyRect);
}


-(void) setupLayers {

	stripMovieButtonArray = [[NSMutableArray alloc] init] ;
	self.backgroundColor = CGColorGetConstantColor(kCGColorClear) ;
#pragma mark this is the size and positioning
	//int numMovies = 3 ;
	int counter = 0 ;
	
	float frameHeight = self.bounds.size.height ;
	
	float mWidth = (frameHeight*16)/9 ;
	float spacing ;//= ([rootLayer bounds].size.width/[urlArray count]) ;
	
	spacing = mWidth+(mWidth*0.045) ;
	
	CGRect aRect ;
	
	for(NSURL* url in stripUrlArray) {
		
		CGPoint aPoint = CGPointMake((mWidth/2)+(counter*spacing), frameHeight/2) ;
		aRect = CGRectMake(0, 0, mWidth, frameHeight) ;
		JigSawMovieButton *but = [[[JigSawMovieButton alloc] initWithObjects:url size:aRect pos:aPoint] autorelease];
		[self addSublayer:but] ;
		
		[stripMovieButtonArray addObject:but] ;
		
		counter ++ ;
	}
	
}

- (void) stripNumberTimerMethod:(NSTimer*)timer {
		// timer to play pause previews
		JigSawMovieButton *butMov = (JigSawMovieButton*)[stripMovieButtonArray objectAtIndex:currentPreview+(3*currentStrip)] ;
		
		QTTime movieDur = [[butMov aMovie] duration] ;
		float time = (float) movieDur.timeValue/movieDur.timeScale ;
		
		if ([stripPreviewIterationTimer isValid]) {
			[stripPreviewIterationTimer invalidate] ;
			stripPreviewIterationTimer = nil ;
			[stripPreviewIterationTimer release];
		}
		stripPreviewIterationTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:time]
															  interval: 0.1	 
																target: self
															  selector: @selector(previewIterationTimerMethod:)
															  userInfo: nil
															   repeats: NO];
		[[NSRunLoop currentRunLoop] addTimer:stripPreviewIterationTimer forMode:NSDefaultRunLoopMode];
		
		[[butMov aMovie] gotoBeginning] ;
		[[butMov aMovie] play] ;	
		
	
	
}

-(void) stripNumber:(int) strip {
	currentPreview = 0 ;
	switch (strip) {
		case 0:
			currentStrip = 0 ;
			break;
		case 1:
			currentStrip = 1 ;
			break;
		case 2:
			currentStrip = 2 ;
			break;
		default:
			break;
	}
	
	// change opactity to full
	[CATransaction begin];
	[CATransaction setValue:[NSNumber numberWithFloat:1.5] forKey:
	 kCATransactionAnimationDuration];
	[CATransaction setValue:[NSNumber numberWithBool: NO] forKey:
	 kCATransactionDisableActions];
	
	for(JigSawMovieButton *jsBut in stripMovieButtonArray) {
		jsBut.opacity = 1.0f ;
	}
	[CATransaction commit];
	
	if ([stripPreviewIterationTimer isValid]) {
		[stripPreviewIterationTimer invalidate] ;
		stripPreviewIterationTimer = nil ;
		[stripPreviewIterationTimer release];
	}
	// we wait a bit before staring the movie interval timer
	stripPreviewIterationTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]
														  interval: 0.1	 
															target: self
														  selector: @selector(stripNumberTimerMethod:)
														  userInfo: nil
														   repeats: NO];
	[[NSRunLoop currentRunLoop] addTimer:stripPreviewIterationTimer forMode:NSDefaultRunLoopMode];

}

-(void) stopPreviewAtIndex:(int) index {
	JigSawMovieButton *butMov = (JigSawMovieButton*)[stripMovieButtonArray objectAtIndex:index] ;
	[[butMov aMovie] gotoEnd] ;
	[[butMov aMovie] stop] ;
	
	//[[butMov aMovie] gotoBeginning] ;
}

-(void) startPreviewAtIndex:(int) index {
	JigSawMovieButton *butMov = (JigSawMovieButton*)[stripMovieButtonArray objectAtIndex:index] ;
	[[butMov aMovie] gotoBeginning] ;
	//[[butMov aMovie] play] ;
}


// stop the previews
-(void) stopAllPreviews {
	if ([stripPreviewIterationTimer isValid]) {
		[stripPreviewIterationTimer invalidate] ;
		stripPreviewIterationTimer = nil ;
		[stripPreviewIterationTimer release] ;
	}
	
	// stop movies and reduce opacity
	[CATransaction begin];
	[CATransaction setValue:[NSNumber numberWithFloat:1.5] forKey:
	 kCATransactionAnimationDuration];
	[CATransaction setValue:[NSNumber numberWithBool: NO] forKey:
	 kCATransactionDisableActions];
	
	for(JigSawMovieButton *jsBut in stripMovieButtonArray) {
		[[jsBut aMovie] gotoEnd] ;
		[jsBut.aMovie stop] ;
		
		jsBut.opacity = kPreviewStopOpacity ;
		
	}
	[CATransaction commit];
}

-(void) startPreviews {
	if (![stripPreviewIterationTimer isValid]) {
		currentPreview = 0 ; //preview play/pause counter
		// timer to play pause previews
		JigSawMovieButton *butMov = (JigSawMovieButton*)[stripMovieButtonArray objectAtIndex:0] ;
		
		
		QTTime movieDur = [[butMov aMovie] duration] ;
		float time = (float) movieDur.timeValue/movieDur.timeScale ;
		
		if ([stripPreviewIterationTimer isValid]) {
			[stripPreviewIterationTimer invalidate] ;
			stripPreviewIterationTimer = nil ;
			[stripPreviewIterationTimer release];
		}
		
		stripPreviewIterationTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:time]
														 interval: 0.1	 
														   target: self
														 selector: @selector(previewIterationTimerMethod:)
														 userInfo: nil
														  repeats: NO];
		[[NSRunLoop currentRunLoop] addTimer:stripPreviewIterationTimer forMode:NSDefaultRunLoopMode];
		
		[[butMov aMovie] gotoBeginning] ;
		[[butMov aMovie] play] ;
	}
	
	
	// change opactity to full
	for(JigSawMovieButton *jsBut in stripMovieButtonArray) {
		jsBut.opacity = 1.0f ;
	}
}

- (void) previewIterationTimerMethod:(NSTimer*) timer {
	//int arrayLength = [stripMovieButtonArray count] ;
	
	int nextPreview = (currentPreview+1)%3 ; // arrayLength if we want to cycle through all of them
	
	JigSawMovieButton *butC = (JigSawMovieButton*)[stripMovieButtonArray objectAtIndex:(currentPreview+(3*currentStrip))] ;
	JigSawMovieButton *butN = (JigSawMovieButton*)[stripMovieButtonArray objectAtIndex:(nextPreview+(3*currentStrip))] ;
	
	currentPreview = nextPreview ;
	
	QTTime movieDur = [[butN aMovie] duration] ;
	float time = (float) movieDur.timeValue/movieDur.timeScale ;
	
	if ([stripPreviewIterationTimer isValid]) {
		[stripPreviewIterationTimer invalidate] ;
		stripPreviewIterationTimer = nil ;
		[stripPreviewIterationTimer release];
	}
	
	stripPreviewIterationTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:time]
													 interval: 0.1	 
													   target: self
													 selector: @selector(previewIterationTimerMethod:)
													 userInfo: nil
													  repeats: NO];
	[[NSRunLoop currentRunLoop] addTimer:stripPreviewIterationTimer forMode:NSDefaultRunLoopMode];
	
	[[butC aMovie] stop] ;
	[[butC aMovie] gotoEnd] ;
	
	//[[butN aMovie] gotoBeginning] ;
	[[butN aMovie] play] ;
}


@end
